#include "stdafx.h"
#include "PsApiFunc.h"
#include "ToolHelp32Func.h"
#include "ProcessItem.h"
#include "SmartHandle.h"

CProcessItem::CProcessItem()
{   
    ::ZeroMemory(&m_stProcessEntry32w, sizeof(m_stProcessEntry32w));
}

CProcessItem::~CProcessItem()
{
    
}

VOID CProcessItem::AddFilter(PFUN_Filter pFun_Filter)
{
    if (pFun_Filter)
    {
        m_PFUN_FilterList.push_back(pFun_Filter);
    }
    
}

VOID CProcessItem::ResetContext()
{
    ::ZeroMemory(&m_stProcessEntry32w, sizeof(m_stProcessEntry32w));
    m_ModEntry32wList.clear();
    m_PFUN_FilterList.clear();
}

DWORD CProcessItem::GetModuleSnapshot(DWORD th32ProcessID)
{
    DWORD dwRet = 0;
    CToolHelp32Func toolHelp32Func;
    if (toolHelp32Func.Initialize())
    {
       CAutoGeneralHandle autoHandle(toolHelp32Func.FunCreateToolhelp32Snapshot(TH32CS_SNAPMODULE, th32ProcessID));
       if (autoHandle.IsValid())
       {
            MODULEENTRY32W moduleEntry32w;
            moduleEntry32w.dwSize = sizeof(moduleEntry32w);

            if (toolHelp32Func.FunModule32FirstW(autoHandle, &moduleEntry32w))
            {
                do 
                {
                    BOOL bAdd = TRUE;
                    for (itePFUN_FilterType ite = m_PFUN_FilterList.begin(); ite != m_PFUN_FilterList.end(); ite++)
                    {
                        if (*ite)
                        {
                            bAdd = (*(*ite))(&moduleEntry32w);
                        }
                    }
                    
                    if (bAdd)
                    {
                        m_ModEntry32wList.push_back(moduleEntry32w);
                    }
                   
                } while (toolHelp32Func.FunModule32NextW(autoHandle, &moduleEntry32w));
            }
       } 
    }

    return dwRet;
}

